package com.alan.smzdm;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class User1 {
	
	public static void user1Op() {
		String result = checkin1();
		JSONObject jsonObject = JSON.parseObject(result);
		String error_msg = jsonObject.getString("error_msg");
		String smzdm_id = jsonObject.getString("smzdm_id");
//		System.out.println(error_msg);
		JSONObject jsonObjectData = jsonObject.getJSONObject("data");
		String en_key = jsonObjectData.getString("en_key");
		String server_time = jsonObjectData.getString("server_time");
		String dataStr = "1582722523866008020316580_" + server_time;
		try {
			byte[] buf = DESUtil.encrypt(dataStr.getBytes(), en_key.getBytes());
			String str = Base64.encodeBase64String(buf);
			String d = URLEncoder.encode(str);
//			System.out.println("d=" + URLEncoder.encode(str));
			lottry1(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String checkin1(){
		String reqData = "token=NjMwOGR8MTQ5NDU3NTY4NXwxNTgyNzIyNTIzfDMzZWUwYTMzNWQwNDE3OTM3ZDkwZjMwZmQyZjBkNjJj&sk=DH0UpTgrhtunO4aqV7eit86DpwFQ6MUW9rQ7UHARO1U%3D&captcha=&f=android&s=ELlqnrrdAvryAryzEXZEx4VXOcRUnTAc&weixin=0&v=7.7&";
		URL url;
		try {
			url = new URL("https://api.smzdm.com/v1/user/checkin");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setRequestProperty("User-agent", "smzdm_android_V7.7 rv:370 (IUNI i1;Android4.4.4;zh)smzdmapp");
			connection.setRequestProperty("Cookie", "sess=MDgzMjV8MTQ5NDg1MDA1MXwxNTgyNzIyNTIzfDQ0NmZmYzAzZWQwZDE4OWI2ZmE0OWZjMThiMTlkYzE1;pid=866008020316580;partner_id=3;partner_id=3;device_id=c1da371ae4890221ce1456eaabb273ce;imei=59e86bbfa6223e5ae3f4440e6892d246;partner_name=smzdm_download;mac=5c:f7:c3:42:47:40;smzdm_id=1582722523;login=1;device_push=1;network=wifi;device_smzdm_version=7.7;device_smzdm_version_code=370;device_s=ELlqnrrdAvryAryzEXZEx4VXOcRUnTAc;device_type=IUNIIUNI i1;device_system_version=4.4.4;device_smzdm=android;rs_id1=;rs_id2=;rs_id3=;rs_id4=;rs_id5=;smzdm_device=android;smzdm_user_source=ELlqnrrdAvryAryzEXZEx4VXOcRUnTAc;smzdm_version=7.7;");
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			OutputStream out = connection.getOutputStream();
			out.write(reqData.getBytes());
			InputStream in = connection.getInputStream();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(in,"utf-8"));
			String tmpStr = "";
			StringBuffer sb = new StringBuffer();
			while((tmpStr=br.readLine())!=null){
				sb.append(tmpStr);
				System.out.println(tmpStr);
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void lottry1(String d){
		URL url;
		String strD = d;//"3yOxc%2FlrYEZSrn8UuDDlVq%2OhWSamSBTD%2FupEJJ%2BT5tGxGB7giHs%2FfA%3D%3D";
		try {
			url = new URL("http://h5.smzdm.com/user/lottery/ajax_draw");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(false);
			connection.setRequestProperty("User-Agent","Mozilla/5.0 (Linux; Android 4.4.4; IUNI i1 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 {smzdm_android_V7.7 rv:370 (IUNI i1;Android4.4.4;zh)smzdmapp}");
			connection.setRequestProperty("Cookie","partner_id=3; partner_name=smzdm_download; smzdm_id=1582722523; partner_id=3; partner_name=smzdm_download; smzdm_id=1582722523; __ckguid=CpB56Es64s3KON6QC7WfON6; __jsluid=98bca616c75d86464636436e34cd4d6e; _ga=GA1.2.608493484.1490685176; smzdm_wordpress_360d4e510beef4fe51293184b8908074=user%3A1582722523%7C1494850051%7C923f0f5ec719a04163ec1b78e48b2daf; smzdm_wordpress_logged_in_360d4e510beef4fe51293184b8908074=user%3A1582722523%7C1494850051%7C0775bbd3dcf5b1f34ce0feb6a7206c60; user-role-smzdm=subscriber; from=android; v=7.7; version=7.7; d="+strD+"; t=f14cb3edea758087d6d8f3f7080ad645; s=ELlqnrrdAvryAryzEXZEx4VXOcRUnTAc; sess=MDgzMjV8MTQ5NDg1MDA1MXwxNTgyNzIyNTIzfDQ0NmZmYzAzZWQwZDE4OWI2ZmE0OWZjMThiMTlkYzE1; device_id=ELlqnrrdAvryAryzEXZEx4VXOcRUnTAc");
			InputStream in = connection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(in,"utf-8"));
			String tmpStr = "";
			StringBuffer sb = new StringBuffer();
			while((tmpStr=br.readLine())!=null){
				sb.append(tmpStr);
				System.out.println(tmpStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
